'''
Created on 23 sep. 2010

@author: christianp
'''
from wx import wx
from PropertyHandler import PropertyHandler

ID_ABOUT = 101
ID_EXIT  = 102

ID_TEXTCTRL = 10000
ID_BUTTON = 10001
ID_LISTBOX = 10002
ID_FOREIGN = 10003
ID_CHECKBOX = 10004
ID_SLIDER = 10005
ID_GAUGE = 10006
ID_TEXT = 10007

class PropertiesScreen(wx.Frame):
    
    pHandler = None

    def __init__(self, parent, ID, title,  call_fit = True, set_sizer = True):
        self.pHandler = PropertyHandler(self)
        
        wx.Frame.__init__(self,parent, title=title, size=(1500,1500))
        panel = wx.Panel(self)

        self.BindMenu()
        self.InitValues(panel)

        sizerMain = wx.FlexGridSizer( 0, 2, 0, 0 )

        boxTilecache = wx.StaticBox(panel, -1, "" )
        #item1  
        sizerTilecache = wx.StaticBoxSizer( boxTilecache, wx.HORIZONTAL )
        # Flex sizer within the TileCache box sizer (item3)
        sizerFlexTC = wx.FlexGridSizer( 0, 1, 0, 0 )
        #item4 
        sizerBoxTC_v = wx.BoxSizer( wx.VERTICAL )
        #item5
        sizerBoxTC_h = wx.BoxSizer( wx.HORIZONTAL )

        sizerBoxTC_v.Add( sizerBoxTC_h, 0, wx.ALIGN_CENTER|wx.ALL, 5 )

        sizerFlexTC.Add( sizerBoxTC_v, 0, 0, 5 )
        sizerTilecache.Add( sizerFlexTC, 0, wx.ALIGN_CENTER_HORIZONTAL, 5 )
        sizerFlexLayer = wx.FlexGridSizer( 0, 2, 0, 0 )
        
        boxLayer2 = wx.StaticBox ( panel, -1, "TileSeeds" )
        sizerLayer2 = wx.StaticBoxSizer( boxLayer2, wx.LEFT )
        sizerLayer2.Add( self.pHandler.lboxTileSeeds,  0, wx.CENTER|wx.ALL, 0)
        
        boxLayer1 = wx.StaticBox ( panel, -1, "Layer" )
        sizerLayer1 = wx.StaticBoxSizer( boxLayer1, wx.HORIZONTAL )
        sizerLayer1.Add( self.pHandler.lboxZoomLevelsLayer,  0, wx.ALIGN_TOP|wx.ALL, 0)
        sizerBoxLayer_v1 = wx.BoxSizer( wx.VERTICAL )
        sizerBoxBbox_v1 = wx.BoxSizer( wx.VERTICAL )
        
        sizerBoxBbox_v1.Add( self.pHandler.lLayerMaxY, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        sizerBoxBbox_h1 = wx.BoxSizer( wx.HORIZONTAL )
        sizerBoxBbox_h1.Add( self.pHandler.lLayerMinX, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        sizerBoxBbox_h1.Add( self.pHandler.lLayerMaxX, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        sizerBoxBbox_v1.Add( sizerBoxBbox_h1, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        sizerBoxBbox_v1.Add( self.pHandler.lLayerMinY, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        
        sizerBoxLayer_v1.Add( sizerBoxBbox_v1, 0, wx.ALIGN_CENTER|wx.ALL, 5 )

        #TODO: replace "layer" with the name of the selected layer
        boxLayer = wx.StaticBox ( panel, -1, "TileSeed" )
        sizerLayer = wx.StaticBoxSizer( boxLayer, wx.HORIZONTAL )
        sizerLayer.Add( self.pHandler.lboxZoomLevelsSeed,  0, wx.ALIGN_TOP|wx.ALL, 0)
        sizerBoxLayer_v = wx.BoxSizer( wx.VERTICAL )
        sizerBoxBbox_v = wx.BoxSizer( wx.VERTICAL )
        
        sizerBoxBbox_v.Add( self.pHandler.lSeedMaxY, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        sizerBoxBbox_h = wx.BoxSizer( wx.HORIZONTAL )
        sizerBoxBbox_h.Add( self.pHandler.lSeedMinX, 0, wx.ALIGN_CENTER|wx.ALL, 5 )       
        sizerBoxBbox_h.Add( self.pHandler.lSeedMaxX, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        sizerBoxBbox_v.Add( sizerBoxBbox_h, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        sizerBoxBbox_v.Add( self.pHandler.lSeedMinY, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        
        sizerBoxLayer_v.Add( sizerBoxBbox_v, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        stdButtonTC = wx.StdDialogButtonSizer()
        btn_apply = wx.Button(panel, wx.ID_APPLY)
        
        self.Bind(wx.EVT_BUTTON, self.pHandler.pEvents.StartSeedRun, btn_apply)
        
        stdButtonTC.AddButton(btn_apply)
        
        btn_cancel = wx.Button( panel, wx.ID_CANCEL )
        self.Bind(wx.EVT_BUTTON, self.pHandler.pEvents.CancelSeedRun, btn_cancel)
        stdButtonTC.AddButton( btn_cancel )
        stdButtonTC.Realize()
        sizerBoxLayer_v.Add( stdButtonTC, 0, wx.GROW|wx.ALL, 5 )
        
        sizerGauge_h = wx.BoxSizer( wx.HORIZONTAL )
        sizerGauge_h.Add( self.pHandler.stxtGaugeDone, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        sizerGauge_h.Add( self.pHandler.gaugeTiles, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        sizerGauge_h.Add( self.pHandler.stxtGaugeRemaining, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        
        sizerBoxLayer_v.Add( sizerGauge_h, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        
        boxTemp = wx.BoxSizer( wx.VERTICAL )
        boxTemp.Add( self.pHandler.labelTilesToBeDone, 0, wx.ALIGN_CENTER|wx.ALL, 5)
        boxTemp.Add( self.pHandler.labelTilesFailed, 0, wx.ALIGN_CENTER|wx.ALL, 5)

        sizerBoxLayer_v.Add(boxTemp, 0, wx.ALIGN_CENTER|wx.ALL, 5 )

        sizerMain.Add( sizerLayer2, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
        
        sizerLayer1.Add( sizerBoxLayer_v1, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        sizerFlexLayer.Add( sizerLayer1, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        
        sizerLayer.Add( sizerBoxLayer_v, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        sizerFlexLayer.Add( sizerLayer, 0, wx.ALIGN_CENTER|wx.ALL, 5 )
        
        sizerTilecache.Add(sizerFlexLayer, 0, wx.ALL, 5 )
        
        sizerMain.Add( sizerTilecache, 0, wx.ALL, 5 )

        sizerMain.AddGrowableCol(1)
        sizerMain.AddGrowableRow(0)

        self.SetAutoLayout(False)
        if set_sizer == True:
            self.SetSizer(sizerMain)
            if call_fit == True:
                sizerMain.SetSizeHints( self )
        
        self.Show(True)
        '''
        menu = wx.Menu()
        editMenu = wx.Menu()
    
        menuItem = editMenu.Append(wx.ID_OK, text="&Process all")
        self.Bind(wx.EVT_MENU,self.pHandler.pEvents.ProcessAllSeeds, menuItem)
        menu.Append(ID_ABOUT, "&About", "More information about this program")
        menu.AppendSeparator()
        
        menuBar = wx.MenuBar()
        menuBar.Append(menu, "&File");
        menuBar.Append(editMenu, "&Process");
        self.SetMenuBar(menuBar)

        wx.EVT_MENU(self, ID_ABOUT, self.pHandler.pEvents.OnAbout)
        wx.EVT_MENU(self, ID_EXIT,  self.pHandler.pEvents.OnClose)
        '''
        
    def InitValues(self, panel):
        self.pHandler.lLayerMinX = wx.TextCtrl( panel, ID_TEXTCTRL, "", wx.Point(15,225), [80,-1], 0 )
        self.pHandler.lLayerMinX.SetToolTip( wx.ToolTip("Min X") )
        self.pHandler.lLayerMinY = wx.TextCtrl( panel, ID_TEXTCTRL, "", wx.Point(100,225), [80,-1], 0 )
        self.pHandler.lLayerMinY.SetToolTip( wx.ToolTip("Min Y") )
        self.pHandler.lLayerMaxX = wx.TextCtrl( panel, ID_TEXTCTRL, "", wx.Point(15,250), [80,-1], 0 )
        self.pHandler.lLayerMaxX.SetToolTip( wx.ToolTip("Max X") )
        self.pHandler.lLayerMaxY = wx.TextCtrl( panel, ID_TEXTCTRL, "", wx.Point(100,250), [80,-1], 0 )
        self.pHandler.lLayerMaxY.SetToolTip( wx.ToolTip("Max Y") )
        
        self.pHandler.lSeedMinX = wx.TextCtrl( panel, ID_TEXTCTRL, "", wx.Point(15,225), [80,-1], 0 )
        self.pHandler.lSeedMinX.SetToolTip( wx.ToolTip("Min X") )
        self.pHandler.lSeedMinY = wx.TextCtrl( panel, ID_TEXTCTRL, "", wx.Point(100,225), [80,-1], 0 )
        self.pHandler.lSeedMinY.SetToolTip( wx.ToolTip("Min Y") )
        self.pHandler.lSeedMaxX = wx.TextCtrl( panel, ID_TEXTCTRL, "", wx.Point(15,250), [80,-1], 0 )
        self.pHandler.lSeedMaxX.SetToolTip( wx.ToolTip("Max X") )
        self.pHandler.lSeedMaxY = wx.TextCtrl( panel, ID_TEXTCTRL, "", wx.Point(100,250), [80,-1], 0 )
        self.pHandler.lSeedMaxY.SetToolTip( wx.ToolTip("Max Y") )
        
        self.pHandler.gaugeTiles = wx.Gauge( panel, ID_GAUGE, 100, wx.DefaultPosition, [100,-1], 0 )
        self.pHandler.stxtGaugeRemaining = wx.StaticText( panel, ID_TEXT, "tiles remaining", wx.DefaultPosition, wx.DefaultSize, 0 )
        
        self.pHandler.labelTilesToBeDone = wx.TextCtrl( panel, ID_TEXTCTRL, "0", wx.Point(15,275), [200,-1], 0 )
        self.pHandler.labelTilesToBeDone.SetToolTip( wx.ToolTip("Total") )
        
        self.pHandler.labelTilesFailed = wx.TextCtrl( panel, ID_TEXTCTRL, "0", wx.Point(15,325), [200,-1], 0 )
        self.pHandler.labelTilesFailed.SetToolTip( wx.ToolTip("Failed") )
        
        self.pHandler.stxtGaugeDone = wx.StaticText( panel, ID_TEXT, "tiles done", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.pHandler.lboxZoomLevelsLayer = wx.ListBox( panel, ID_LISTBOX, wx.Point(500,500), [80,300], [""] , wx.LB_SINGLE )
        self.pHandler.lboxZoomLevelsSeed = wx.ListBox( panel, ID_LISTBOX, wx.Point(500,500), [80,300], [""] , wx.LB_SINGLE )
        self.pHandler.lboxTileSeeds = wx.ListBox( panel, ID_LISTBOX, wx.Point(500,500), [150,330], [""] , wx.LB_SINGLE )
        
        self.pHandler.lboxTileSeeds.Bind(wx.EVT_LISTBOX, self.pHandler.pEvents.TileSeedSelected)
        self.pHandler.lboxZoomLevelsSeed.Bind(wx.EVT_LISTBOX, self.pHandler.pEvents.RemoveZoomLvl)
        self.pHandler.lboxZoomLevelsLayer.Bind(wx.EVT_LISTBOX, self.pHandler.pEvents.AddZoomLvl)

    def BindMenu(self):
        wx.EVT_MENU(self, wx.ID_EXIT,  self.pHandler.pEvents.OnQuit)
        self.Bind(wx.EVT_CLOSE, self.pHandler.pEvents.OnClose)
        
        MenuBar = wx.MenuBar()
        FileMenu = wx.Menu()
        #TileSeedMenu = wx.Menu()
        
        removeSeedItem = FileMenu.Append(wx.ID_REMOVE, text="&Remove TileSeed")
        
        saveMenuItem = FileMenu.Append(wx.ID_SAVEAS, text="&Save")
        openMenuItem = FileMenu.Append(wx.ID_OPEN, text="&Open")
        FileMenu.AppendSeparator()
        quitMenuItem = FileMenu.Append(wx.ID_EXIT, text="&Quit")
        
        
        '''
        startMenuItem = TileSeedMenu.Append(wx.ID_OK, text="&Start caching")
        pauseMenuItem = TileSeedMenu.Append(wx.ID_EXIT, text="&Pause caching")
        cancelMenuItem = TileSeedMenu.Append(wx.ID_EXIT, text="&Cancel caching")
        TileSeedMenu.AppendSeparator()
        propertiesMenuItem = TileSeedMenu.Append(wx.ID_PROPERTIES, text="&Properties")
        '''
        
        self.Bind(wx.EVT_MENU, self.pHandler.pEvents.OnSave, saveMenuItem)
        self.Bind(wx.EVT_MENU, self.pHandler.pEvents.OnOpen, openMenuItem)
        self.Bind(wx.EVT_MENU, self.pHandler.pEvents.RemoveTileSeedFromList, removeSeedItem)
        '''
        self.Bind(wx.EVT_MENU, self.OnStart, startMenuItem)
        self.Bind(wx.EVT_MENU, self.OnPause, pauseMenuItem)
        self.Bind(wx.EVT_MENU, self.OnCancel, cancelMenuItem)
        self.Bind(wx.EVT_MENU, self.OnTileSeedProperties, propertiesMenuItem)
        '''
        MenuBar.Append(FileMenu, "&File")
        #MenuBar.Append(TileSeedMenu, "&TileSeed")
        self.SetMenuBar(MenuBar)
        '''
        MenuBar = wx.MenuBar()
        FileMenu = wx.Menu()
        quitMenuItem = FileMenu.Append(wx.ID_EXIT, text="&Quit")

        self.Bind(wx.EVT_CLOSE, self.pHandler.pEvents.OnClose)
        MenuBar.Append(FileMenu, "&File")

        self.SetMenuBar(MenuBar)'''
        
    